#include "Buffers.hpp"

namespace Buffer {

BufferPool::BufferPool() {}

BufferPool &BufferPool::GetInstance() {
  static BufferPool instance;
  return instance;
}

BufferRef BufferPool::CreateABuffer(const BufferType &type) {
  BufferBase *buffer = new BufferBase(type);
  buffer->Create();
  buffers.push_back(buffer);
  return std::shared_ptr<BufferBase>(buffer);
}

void BufferPool::RemoveBuffer(GLuint id) {
  buffers.remove_if([id](BufferBase *buffer) -> bool {
    if (buffer->GetID() == id) {
      delete buffer;
      return true;
    }
    return false;
  });
}

bool BufferPool::Contains(GLuint id) const {
  for (auto it = buffers.cbegin(); it != buffers.cend(); it++) {
    if ((*it)->GetID() == id)
      return true;
  }
  return false;
}

void BufferPool::Clear() {
  for (auto &val : buffers) {
    val->Delete();
  }
}

BufferPool::~BufferPool() { Clear(); }

} // namespace Buffer